package com.qin.EducationalManger.score.servlet;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.mysql.cj.x.protobuf.MysqlxCrud.Update;
import com.qin.EducationalManger.course.model.CourseModel;
import com.qin.EducationalManger.course.model.StudentCourseModel;
import com.qin.EducationalManger.course.model.namesModel;
import com.qin.EducationalManger.course.service.Imp.CourseServiceImp;
import com.qin.EducationalManger.score.model.ScoreModel;
import com.qin.EducationalManger.score.model.studentScoreModel;
import com.qin.EducationalManger.score.service.ScoreService;
import com.qin.EducationalManger.score.service.Imp.ScoreServiceImp;
import com.sun.javafx.sg.prism.NGShape.Mode;

/**
 * Servlet implementation class ScoreServlet
 */
@WebServlet("/ScoreServlet")
public class ScoreServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ScoreServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
//		doGet(request, response);
		String action = request.getParameter("action");
		
		Object res="";
		switch (action) {
	
		case "添加":
			
			break;
		case "修改":
		
		 String reu =	 update(request)+"";
		 response.getWriter().write(reu);
		   break;
		case "删除":
			String ree = delete(request)+"";
			response.getWriter().write(ree);
			
			break;
		case"查询":
			Collection<studentScoreModel> rec = selectCourse(request);
			
			
			
			response.getWriter().write(new JSONArray(rec).toString());
			
			
		default:
			break;
		}

		
		
		

	}
	
	//修改成绩 信息 
	private Integer  update(HttpServletRequest request) {
		// TODO Auto-generated method stub
		String studentId = request.getParameter("studentId");
		String courseId = request.getParameter("courseId");
		String score_ =request.getParameter("score");
		String gradePoint_ = request.getParameter("gradePoint");
		
		System.out.println("-----测试------");
		System.out.println(studentId);
		System.out.println(courseId);
		System.out.println(score_);
		System.out.println(gradePoint_);
		
		System.out.println("-----测试------");
		
		ScoreModel model = new ScoreModel();
		model.setCourseId(courseId);
		model.setStudentId(studentId);
		Double score =null;
		if(score_!=null&&score_.intern().isEmpty()==false) {
			score=Double.parseDouble(score_);
		}
		Double gradePoint =null;
		if(gradePoint_!=null&&gradePoint_.intern().isEmpty()==false) {
			gradePoint=Double.parseDouble(gradePoint_);
		}
		model.setScore(score);
		model.setGradePoint(gradePoint);
		
		
		
		
		return service.update(model);
	}

	// 查询存在的课程信息
	private List<studentScoreModel> selectCourse(HttpServletRequest request) {
		// TODO Auto-generated method stub
		String studentId =request.getParameter("studentId");
		String courseId = request.getParameter("courseId");	
		
		ScoreModel model = new ScoreModel();
		model.setCourseId(courseId);
		model.setStudentId(studentId);
		
		List<studentScoreModel>  scores = service.selectScore(model);
		
		return scores;
	}
	private static ScoreServiceImp service = new ScoreServiceImp();
 

	
	
	private Integer delete(HttpServletRequest request) {
		// TODO Auto-generated method stub
	
		String courseName = request.getParameter("courseName");
			

		
		ScoreModel model = new ScoreModel();
		
		
	
	
		return service.delete(model);
		
		
		
	}

	
//	private HashMap<String, Object> select(HttpServletRequest request) {
//		// TODO Auto-generated method stub
//		
//		
//		String courseId = request.getParameter("courseId");
//		
//		
//		
//		if(courseId==null) {
//			return null;
//		}
//		
//		
//	     String  courseIds[] =	courseId.split(" ");
//		
//		
//		
//		List<StudentCourseModel> models = service.select(courseIds);
//		   
//		HashMap<String, Object> list = new HashMap<String, Object>();
//		
//		
//		list.put("lists", models);
//		return  list;
//		 
//		
//	
//	}

}
